package com.tool4j.util.http;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
public class HttpUtil {

    private static final OkHttpClient CLIENT = new OkHttpClient.Builder()
            .connectTimeout(120, TimeUnit.SECONDS)//设置连接超时时间
            .readTimeout(120, TimeUnit.SECONDS)//设置读取超时时间
            .build();

    public static String get(String url) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            Response response = CLIENT.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            log.error("请求失败", e);
        }
        return null;
    }

    public static String post(String url, Object params) {
        return post(url, params, null);
    }

    public static String post(String url, Object params, Map<String, String> header) {
        try {
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body;
            if (params != null) {
                String paramsJson = JSONUtil.toJsonStr(params);
                body = RequestBody.create(mediaType, paramsJson);
            } else {
                body = RequestBody.create(mediaType, "");
            }
            Request.Builder builder = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json");
            if (CollUtil.isNotEmpty(header)) {
                header.forEach(builder::addHeader);
            }
            Request request = builder.build();
            Response response = CLIENT.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            log.error("请求失败", e);
        }
        return null;
    }

    /**
     * POST方式提交表单
     */
    public static String postForm(String url, FormBody formBody) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .post(formBody)
                    .build();
            Response response = CLIENT.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            log.error("请求失败", e);
        }
        return null;
    }

    /**
     * POST方式提交表单
     */
    public static String postForm(String url, MultipartBody formBody, Map<String, String> headers) {
        try {
            Request.Builder builder = new Request.Builder()
                    .url(url)
                    .post(formBody);
            headers.forEach(builder::addHeader);
            Request request = builder.build();
            Response response = CLIENT.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            log.error("请求失败", e);
        }
        return null;
    }
}
